package com.android.showRoomApp.util;

import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketUtils {

    // 检查 socket connection
    public static boolean checkSocketConnect(String ip, int port) {

        try {
            Socket socket = new Socket(ip, port);
            socket.close();

            return true;

        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    // 发送socket data
    public static boolean sentSocketData(String ip, int port,  String sendData) {


        try {
            Socket socket = new Socket(ip, port);

            // socket.getInputStream()
            DataOutputStream writer = new DataOutputStream(socket.getOutputStream());

            String sendingText = sendData;
            byte[] data = sendingText.getBytes();
            Log.i("zjq",data.toString());
            writer.write(data);

            System.out.println("Sending: " + sendingText);

//            DataInputStream reader = new DataInputStream( socket.getInputStream());
//
//            String gotData = reader.readUTF();
//
//            System.out.println("Got: " + gotData);

            socket.close();


//            if(gotData.equalsIgnoreCase(sendingText)) {
                return true;
//            } else {
//                return false;
//            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;

        }

    }
}
